<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="dialogFormRef" :model="state" :rules="state.rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="需求名称" prop="ruleForm.name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入需求名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="需求号" prop="ruleForm.num">
							<el-input v-model="state.ruleForm.num" placeholder="请输入需求号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="备注" prop="ruleForm.remarks">
							<el-input v-model="state.ruleForm.remarks" placeholder="请输入备注" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="需求状态"  prop="ruleForm.status">
							<el-radio-group v-model="state.ruleForm.status">
								<el-radio label="0">未上线</el-radio>
								<el-radio label="1">上线</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit(dialogFormRef)" size="default">{{ state.dialog.submitTxt
					}}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemUserDialog">
import { reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus'
import { requirementApi } from './api';
import { ElMessage } from 'element-plus';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 引入 api 请求接口
const api = requirementApi();
// 定义变量内容
const dialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id: '',//主键id
		name: '', // 需求名称
		num: '', // 需求号
		status:'',//需求状态
		remarks:''//备注
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	rules: {
		'ruleForm.name': { required: true, message: '请输入需求名称', trigger: 'blur' },
		'ruleForm.num': { required: true, message: '请输入需求号', trigger: 'blur' },
		'ruleForm.status': { required: true, message: '请选择需求状态	', trigger: 'blur' },
	},
});


// 打开弹窗
const openDialog = (type: string, row: any) => {
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改需求信息';
		state.dialog.submitTxt = '修 改';
	} else {
		//新增时 主键id置为空
		state.ruleForm.id = '';
		state.dialog.title = '新增需求';
		state.dialog.submitTxt = '新 增';
	}
	state.dialog.isShowDialog = true;

};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return
	formEl.validate((valid) => {
		if (valid) {
			// console.log('校验通过！')
			api.save(state.ruleForm).then(
				(res) => {
					if (res.data.code === "000000") {
						ElMessage.success(res.data.message);
						closeDialog(); // 关闭弹窗
						emit('refresh');
					} else {
						ElMessage.warning(res.data.message);
					}
				}
			)
		} else {
			// console.log('校验未通过！')
			return false
		}
	})
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
